package com.runfast.waimai.utils.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.runfast.paotui.dao.model.Order;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;
import com.runfast.waimai.dao.model.WaiMaiRunfastShopper;
import com.runfast.waimai.utils.websocket.DiaoDuWebsocketController;
import com.runfast.waimai.utils.websocket.PaoTuiDiaoDuWebsocketController;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public  class BusinessOrderReceiver {
    @Autowired
    private DiaoDuWebsocketController diaoDuWebsocketController;
    @Autowired
    private PaoTuiDiaoDuWebsocketController paoTuiDiaoDuWebsocketController;
    /**商家发送的外卖订单信息*/
    @RabbitListener(bindings=@QueueBinding(value=@Queue,exchange=@Exchange(name="business_order_exchange",type =ExchangeTypes.FANOUT)))
    @RabbitHandler
    public void process(String order1) {
        //System.out.println(order1);
        RunfastGoodsSellRecord  order=null;
        order=JSON.parseObject(order1,RunfastGoodsSellRecord.class);
        //BeanUtils.copyProperties(order1,order);
        System.out.println("商家发送的外卖订单信息:"+order);
        if(diaoDuWebsocketController.webSockets !=null &&diaoDuWebsocketController.webSockets.size()>0){
            diaoDuWebsocketController.broadcast(order, null,null);
        }
    }
    /**骑手的信息*/
    @RabbitListener(bindings=@QueueBinding(value=@Queue,exchange=@Exchange(name="shopper_exchange",type =ExchangeTypes.FANOUT)))
    @RabbitHandler
    public void process1(String shopper1) {
        //System.out.println(shopper1);
        WaiMaiRunfastShopper shopper=null;
        shopper=JSON.parseObject(shopper1,WaiMaiRunfastShopper.class);
        System.out.println("骑手的信息:"+shopper);
        if(diaoDuWebsocketController.webSockets !=null &&diaoDuWebsocketController.webSockets.size()>0){
            diaoDuWebsocketController.broadcast(null, shopper,null);
        }
        if(paoTuiDiaoDuWebsocketController.webSockets1 !=null &&paoTuiDiaoDuWebsocketController.webSockets1.size()>0){
            paoTuiDiaoDuWebsocketController.paotuiBroadcast(null, shopper,null);
        }
    }
    /**骑手发送的外卖订单信息*/
    @RabbitListener(bindings=@QueueBinding(value=@Queue,exchange=@Exchange(name="wm_shopper_order_exchange",type =ExchangeTypes.FANOUT)))
    @RabbitHandler
    public void process3(String order1) {
        //System.out.println(order1);
        RunfastGoodsSellRecord  order=null;
        order=JSON.parseObject(order1,RunfastGoodsSellRecord.class);
        //BeanUtils.copyProperties(order1,order);
        System.out.println("骑手发送的外卖订单信息:"+order);
        if(diaoDuWebsocketController.webSockets !=null &&diaoDuWebsocketController.webSockets.size()>0){
            diaoDuWebsocketController.broadcast(order, null,null);
        }
    }
    /**骑手发送的跑腿订单信息*/
    @RabbitListener(bindings=@QueueBinding(value=@Queue,exchange=@Exchange(name="pt_shopper_order_exchange",type =ExchangeTypes.FANOUT)))
    @RabbitHandler
    public void process4(String order1) {
        //System.out.println(order1);
        Order order=null;
        order=JSON.parseObject(order1,Order.class);
        //BeanUtils.copyProperties(order1,order);
        System.out.println("骑手发送的跑腿订单信息："+order);
        if(paoTuiDiaoDuWebsocketController.webSockets1 !=null &&paoTuiDiaoDuWebsocketController.webSockets1.size()>0){
            paoTuiDiaoDuWebsocketController.paotuiBroadcast(null, null,order);
        }
    }


    /**用户发送的外卖订单信息*/
    @RabbitListener(bindings=@QueueBinding(value=@Queue,exchange=@Exchange(name="wm_order_exchange",type =ExchangeTypes.FANOUT)))
    @RabbitHandler
    public void process7(Message order1) {
        String json=new String(order1.getBody());
        //System.out.println(json);
        RunfastGoodsSellRecord  order= null;
        order=JSON.parseObject(json,RunfastGoodsSellRecord.class);
        System.out.println("用户发送的外卖订单信息:"+order);
        if(diaoDuWebsocketController.webSockets !=null &&diaoDuWebsocketController.webSockets.size()>0){
            diaoDuWebsocketController.broadcast(order, null,null);
        }
    }
    /**用户发送的跑腿订单信息*/
    @RabbitListener(bindings=@QueueBinding(value=@Queue,exchange=@Exchange(name="pt_order_exchange",type =ExchangeTypes.FANOUT)))
    @RabbitHandler
    public void process6(Message order1) {
        String json=new String(order1.getBody());
        //System.out.println(json);
        Order  order= null;
        order=JSON.parseObject(json,Order.class);
        System.out.println("用户发送的跑腿订单信息:"+order);
        if(paoTuiDiaoDuWebsocketController.webSockets1 !=null &&paoTuiDiaoDuWebsocketController.webSockets1.size()>0){
            paoTuiDiaoDuWebsocketController.paotuiBroadcast(null, null,order);
        }
    }


    /**后台自己发送的外卖订单信息*/
    @RabbitListener(bindings=@QueueBinding(value=@Queue,exchange=@Exchange(name="waimaiServer_order_exchange",type =ExchangeTypes.FANOUT)))
    @RabbitHandler
    public void process8(String order1) {
        //System.out.println(order1);
        RunfastGoodsSellRecord  order=null;
        order=JSON.parseObject(order1,RunfastGoodsSellRecord.class);
        System.out.println("后台自己发送的外卖订单信息:"+order);
        if(diaoDuWebsocketController.webSockets !=null &&diaoDuWebsocketController.webSockets.size()>0){
            diaoDuWebsocketController.broadcast(order, null,null);
        }
    }
    /**后台自己发送的外卖骑手信息*/
    @RabbitListener(bindings=@QueueBinding(value=@Queue,exchange=@Exchange(name="server_shopper_exchange",type =ExchangeTypes.FANOUT)))
    @RabbitHandler
    public void process9(String shopper1) {
        //System.out.println(shopper1);
        WaiMaiRunfastShopper shopper=null;
        shopper=JSON.parseObject(shopper1,WaiMaiRunfastShopper.class);
        System.out.println("后台自己发送的外卖骑手信息:"+shopper);
        if(diaoDuWebsocketController.webSockets !=null &&diaoDuWebsocketController.webSockets.size()>0){
            diaoDuWebsocketController.broadcast(null, shopper,null);
        }
        if(paoTuiDiaoDuWebsocketController.webSockets1 !=null &&paoTuiDiaoDuWebsocketController.webSockets1.size()>0){
            paoTuiDiaoDuWebsocketController.paotuiBroadcast(null, shopper,null);
        }
    }

    /**后台自己发送的跑腿订单信息*/
    @RabbitListener(bindings=@QueueBinding(value=@Queue,exchange=@Exchange(name="paotuiServer_order_exchange",type =ExchangeTypes.FANOUT)))
    @RabbitHandler
    public void process10(String order1) {
        //System.out.println(order1);
        Order  order=null;
        order=JSON.parseObject(order1,Order.class);
        System.out.println("后台自己发送的跑腿订单信息:"+order);
        if(paoTuiDiaoDuWebsocketController.webSockets1 !=null &&paoTuiDiaoDuWebsocketController.webSockets1.size()>0){
            paoTuiDiaoDuWebsocketController.paotuiBroadcast(null, null,order);
        }
    }
}
